package _5_proxy

import "fmt"

//DBConnector 数据库连接器
type DBConnector interface {
	Connect(addr string) bool
}

//MySqlConnector mysql 连接器
type MySqlConnector struct {
	Name string
	Addr string
}

func (m *MySqlConnector) Connect(addr string) bool {
	fmt.Println("MySqlConnector...")
	return true
}

//DBConnectorProxy 数据连接代理
type DBConnectorProxy struct {
	RealConnector DBConnector
	Name          string
}

func (*DBConnectorProxy) validate(addr string) bool {
	if len(addr) > 0 {
		fmt.Println("DBConnectorProxy 验证成功!")
		return true
	}
	return false
}

func (d *DBConnectorProxy) Connect(addr string) bool {
	if d.validate(addr) {
		return d.RealConnector.Connect(addr)
	}
	return false
}

type AppClient struct {
	conn DBConnector
	name string
}

func (a AppClient) Start() {
	fmt.Println("启动client...连接数据库")
	a.conn.Connect("127.0.0.1")
	fmt.Println("启动client完成")
}
